//alert("调用test01.js")

var x, length
x = 5
length = 6
document.write(x + length)
document.write(x + "123")
document.write("<h1>这是标题1</h1>")
document.write("<p>这是段落</p>")
document.write("<button type='button' onclick=\"alert('欢迎!')\">点击我</button>")
document.write("<button type='button' onclick='modifycontent()'>修改内容</button>")
document.write("<button type='button' onclick='modifycolor()'>修改颜色</button>")

document.write("<input id='id_input' type='text'>")
document.write("<button type='button' onclick='check_input()'>校验输入</button>")

document.write("<br />")
document.write("<h4>javascript数据类型</h4>")
document.write("String,Number,Boolean,Array,Object,Null,Undefined")

document.write("<br />")
x=123e5
document.write("变量x=123e5的值为")
document.write(x)

document.write("<br />")
x=123e-5
document.write("变量x=123e-5的值为")
document.write(x)

document.write("<br />")
document.write("<h4>javascript数组</h4>")
document.write("new Array")
document.write("<br />")
var cars = new Array()
cars[0] = "Saab"
cars[1] = "Jeep"
cars[2] = "BMW"
document.write("cars[0]="+cars[0])
document.write("<br />")

document.write("new Array('Saab','Jeep','BMW')")
document.write("<br />")
cars = new Array("Saab","Jeep","BMW")
document.write("cars[0]="+cars[0])
document.write("<br />")

document.write('cars = ["bench","bmw","jeep"]')
document.write("<br />")
cars = ["bench","bmw","jeep"]
document.write("cars[1]="+cars[1])
document.write("<br />")

document.write("<h4>javascript对象:</h4>")
var person = {firstname:"zha",lastname:"daolong"}
document.write(person.firstname)
document.write(person.lastname)
document.write("<br />")
document.write(person["firstname"])
document.write(person["lastname"])
document.write("<br />")
document.write("对象属性作为方法，引用成员变量需要加this.")
document.write("<br />")
var person = {
    firstname:"zha",
    lastname:"daolong",
    fullname:function(){
        return this.firstname+this.lastname // 必须要添加this
    }
}
document.write(person.fullname())

document.write("<h4>undefined</h4>")
var undef = undefined
document.write(undef)
document.write("<br />")

document.write("<h5>在函数内部未使用var申明的变量也是全局变量</h5>")
function golbalvar(){
    g_function_var = "g_function_var_str"
}
golbalvar()
document.write(g_function_var)
document.write("<br />")

document.write("<h5 id='id_test_event \
                    onclick='test_event_onclick()' \
                    onmouseover='test_event_onmouseover()' \
                    onmouseout='test_event_onmouseout()'>HTML元素事件</h5>")

document.write("<h4>javascript字符串</h4>")
var str = "名字:\"zhadaolong\""
document.write(str)
document.write("<br/>")
document.write("字符串长度:",str.length)
document.write("<br/>")
document.write(typeof(str))
document.write("<br/>")

var str = new String("new一个字节符串，此为字符串对象")
document.write(str)
document.write("<br/>")
document.write(typeof(str))
document.write("<br/>")
document.write(str[4])
document.write("<br/>")

var x = 5
if(x == 5)
    document.write("x==5<br/>")
else
    document.write("x!=5<br/>")

var str = new String("5")   // 字符串对象
var x = "5"                 // 字符串
if(x === str)
    document.write("x===5，绝对相等，类型即值都相等<br/>")
else
    document.write("x===5，绝对相等，类型或值不相等<br/>")

if(x !== str)
    document.write("x!==5，不绝对相等，类型或值不相等<br/>")
else
    document.write("x!==5，不绝对相等，类型或值都相等<br/>")

switch(x){
    case "3":
    document.write("x=3<br/>")
    break;
    case "4":
    document.write("x=4<br/>")
    break;
    case "5":
    document.write("x=5<br/>")
    break;
    default:
    document.write("x=unknow<br/>")
    break;    
}

var psn = ["ie","fox","chrome"]
for (var i=0; i<psn.length; ++i){
    document.write(psn[i]+"<br/>")
}

for (var i in psn){
    document.write(i+": ")
    document.write(psn[i]+"<br/>")
}

var person = {fname:"zha",lname:"daolong",age:"30"}
for (i in person){
    document.write(i+": ")
    document.write(person[i]+"<br/>")
}

var i=0
while(i<5){
    document.write(i+",")
    ++i
}
document.write("<br/>")

var i=0
do{
    document.write(i+",")
    i++
}while(i<5)

for (i=0; i<5; ++i){
    if(i==4){
        document.write("break<br/>")
        break
    }
    if(i==2){
        document.write("continue<br/>")
        continue
    }
    document.write(i+"<br/>")
}

for (i=0; i<5; ++i){
    //if(i == 2)
    //    break to_break
    document.write("break 到label i="+i+"<br/>")
}

to_break:
document.write("i=2,break to this<br/>")

document.write("<br/>")
document.write("typeof undefined="+typeof(undefined)+"<br/>")
document.write("typeof null="+typeof(null)+"<br/>")

var mystr = new String
document.write("String constructor: "+mystr.constructor.toString()+"<br/>")

var myArry = new Array()
document.write("Array constructor: "+myArry.constructor.toString()+"<br/>")

//var myData = new Data()
//document.write("Data constructor: "+myData.constructor.toString()+"<br/>")

var myNum = 345
document.write("(324).toString:"+(324).toString()+"<br/>")
document.write("myNum.toString:"+myNum.toString()+"<br/>")

document.write("<h5>Number 字符串转为数字</h5>")
var num = Number("2563")
document.write("Number(\"2563\") = "+num+"<br/>")

document.write("<h5>javascript正则表达式，/两斜扛中间的内容为正则表达式/</h5>")
var str = "document_write"
document.write(str.search(/write/))
document.write("<br/>")
document.write(str.replace(/write/,"read"))
document.write("<br/>")

document.write("<h5>javascript错误，throw,try,catch</h5>")
try{
    errorcode
}
catch(err)
{
    alert(err)
}

document.write("<h5>javascript调试，使用浏览器调试，打开开发者工具</h5>")

document.write("<h5>javascript变量提升，函数及变量的声明都将被解析器提升到函数的最顶部</h5>")
document.write("<p>但初始化不会被解析器提升</p>")
var x = 5;      // 初始化x
var y;          // 声明y，但未初始化
document.write("x="+x,", y="+y+"<br/>")
y = 6;

document.write("<h5>javascript使用严格模式(use strict)</h5>")
document.write("<p>不允许使用未定义的变量等...</p>")

document.write("<p>null用于对象，undefined用于变量，属性或方法</p>")

document.write("<form name='myForm' onsubmit='validateForm()'>")
document.write("请输入1-9数字：<input type='text' name='fname'>")
document.write("<input type='submit' value='确定'>")
document.write("</form>")


document.write("<p>JavaScript JSON字符串转为javascript对象</p>")
var jsonfile = "{'name':'zha','age':30, 'add':'shenzhen'}"
//var jsonobj = JSON.parse(jsonfile)
//document.write("<p>jsonobj.name: "+jsonobj.name+"</p>")


document.write("<p>javascript:void(0)，指示要计算机一个值，但不返回值</p>")
var a,b,c;
a = void(b=3,c=5)
document.write("<p>a= "+a+",b= "+b+",c= "+c+"</p>")

document.write("<p>javascript函数默认参数，未提供为undefined</p>")
function myDefaultFunc(x, y){
    if(y == undefined)
        document.write("<p>y is undefined</p>")
}
myDefaultFunc(10)

document.write("<p>javascript函数参数，Arguments对象</p>")
function sumAll(){
    var i, sum=0;
    for(i=0; i<arguments.length; i++){
        sum += arguments[i];
        document.write("<p>sum="+sum+"</p>")
    }
    return sum;
}

sumret = sumAll(1,234,456,20,20);
document.write("<p>sumret= "+sumret+"</p>")

var x = 2;
function testthis(){
    this.x = 1;
}
document.write("<p>执行testthis()函数前x = "+x+"</p>")    // 2
testthis()
document.write("<p>执行testthis()函数后x = "+x+"</p>")    // 1
document.write("<p>说明函数内部的this是全局调用函数的全局对象</p>")

// 再定义一个一样的函数，会覆盖上面定义的testthis函数
//function testthis(){
//    this.x = 2;
//}
var x = 3;
function testthis2(){
    this.x = 1;
}
var obj = new testthis2();
document.write("<p>obj.x为对象调用函数，所以函数内的this.x不是全局的x, obj.x="+obj.x+"</p>")
document.write("<p>全局的x = "+x+"</p>")

document.write("<p>函数方法call或apply调用函数</p>")
mySumAll1 = sumAll.call(this, 1,2,3);
mySumAll2 = sumAll.apply(this, [1,2,3]);
document.write("<p>mySumAll1 = "+typeof(mySumAll1)+"</p>")
document.write("<p>mySumAll2 = "+typeof(mySumAll2)+"</p>")
document.write("<p>mySumAll1 = "+mySumAll1+"</p>")
document.write("<p>mySumAll2 = "+mySumAll2+"</p>")












function validateForm(){
    var x = document.forms["myForm"]["fname"].value;
    alert(x)
}

function modifycontent() {
    var x = document.getElementById("demo")
    x.innerHTML = "此段落已经被js脚本替换掉了"
}
function modifycolor() {
    var x = document.getElementById("demo")
    x.style.color = "#00FF00"
}

function check_input() {
    var x = document.getElementById("id_input").value
    if (x == "" || isNaN(x))
        alert("输入的不是数字")
    else
        alert("输入的为数字")
}

function test_event_onclick(){
    alert("鼠标点击元素")
}

function test_event_onmouseover(){
    var x = document.getElementById("id_test_event")
    x.innerHTML = "HTML元素事件，onmouseover"
}

function test_event_onmouseout(){
    var x = document.getElementById("id_test_event")
    x.innerHTML = "HTML元素事件，onmouseout"
}
